var qqmapsdk,
    app = getApp(),
    util = require('../../utils/util.js'),
    QQMapWX = require('../../lib/qqmap-wx-jssdk.js'),
    _location = ''

Page({
    data: {
        showLoading: !0,
        weizhi: '附近',
        hotel: [],
        Recommend: '1',
        classifyid: 1,
        tabs: [1, 2, 3],
        sliderLeft: '10%',
        sliderOffset: '0'
    },
    onLoad: function (i) {
        var n = this
        var t = decodeURIComponent(i.scene)
        if (null != t) var a = t
        if (null != i.upper_partner) a = i.upper_partner
        var fid = i.scene != undefined ? t : 0
        this.setData({
            fid: fid
        })
        app.globalData.invite_uid = a
        console.log(app.globalData.invite_uid)
        app.getUrl(n)
        app.getSystem(n)
        app.getUserInfo()
        n.setData({
            start: app.util.time(),
            end: app.util.addDate(app.util.time(), 28)
        })

        n.dwCity()
        console.log(wx.getStorageSync('longitude'), 963)
        this.getList()
    },
    getList() {
        const n = this
        wx.getLocation({
            type: 'wgs84',
            success: function (t) {
                var e = t.latitude,
                    a = t.longitude;
                console.log(t, '99999定位')
                wx.setStorageSync('longitude', a) // 经纬度存到缓存
                wx.setStorageSync('latitude', e)
                var d = []
                app.util.request({
                    url: 'entry/wxapp/JdList',
                    cachetime: '0',
                    data: {
                        cityName: wx.getStorageSync('cityName'),
                        rec: '1', //预留推荐字段
                        longitude: wx.getStorageSync('longitude') || a,
                        latitude: wx.getStorageSync('latitude') || e
                    },
                    success: function (t) {
                        if (0 < t.data.length) {
                            d = d.concat(t.data)
                            // for (var e1 = 0; e1 < d.length; e1++) {
                            //     var a = d[e1].coordinates.split(',');
                            //     (d[e1].lat2 = a[0]), (d[e1].lng2 = a[1])
                            //     var i = e.data.lat1,
                            //         n = e.data.lng1,
                            //         o = Number(a[0]),
                            //         r = Number(a[1])
                            //     // d[e1].distance = app.util.location(i, o, n, r)
                            //     // d[e1].distance = (d[e1]?.distance?.split('.')[0])
                            // }
                            n.setData({
                                hotel: d
                            })
                        } else {
                            n.setData({
                                hotel: []
                            })
                        }
                    }
                })
            },
            fail: function (t) {
                wx.hideLoading(),
                    (_location = 1),
                    wx.showModal({
                        title: '授权提示',
                        content: '您取消了本次授权，小程序将无法正常使用，请点击确定或者在我的-授权管理中进行授权，再次重新进入小程序即可',
                        showCancel: !0,
                        cancelText: '取消',
                        cancelColor: '#333',
                        confirmText: '确定',
                        confirmColor: '#333',
                        success: function (t) {},
                        fail: function (t) {},
                        complete: function (t) {}
                    })
            }
        })
    },
    date: function (t) {
        var e = wx.getStorageSync('day1'),
            a = wx.getStorageSync('day2'),
            i = wx.getStorageSync('day'),
            n = app.util.time()
        if ('' == e) {
            var o = app.util.time()
            wx.setStorageSync('datein', o)
        } else if (e < n) o = n
        else o = e
        if ('' == a) var s = app.util.addDate(n, 1)
        else {
            var r = app.util.addDate(n, 1)
            if (a < r) s = r
            else s = a
        }
        i = app.util.day(s, o)
        wx.setStorageSync('day1', o),
            wx.setStorageSync('day2', s),
            wx.setStorageSync('day', i),
            this.setData({
                datein: o,
                dateout: s,
                time: i,
                current_date: o
            })
    },
    refresh: function (t) {
        var e = this
        var d = [],
            g = e.data.Recommend
        app.util.request({
                url: 'entry/wxapp/GetNav',
                cachetime: '0',
                success: function (t) {
                    e.setData({
                        nav: t.data
                    })
                }
            }),
            app.util.request({
                url: 'entry/wxapp/GetSystem',
                cachetime: '0',
                success: function (t) {
                    wx.setStorageSync('platform', t.data)
                    // wx.setStorageSync("cityName", "")
                    wx.setNavigationBarTitle({
                        title: t.data.pt_name
                    });
                    (qqmapsdk = new QQMapWX({
                        key: t.data.map_key
                    })),
                    '2' == t.data.type
                },
                complete: function (t) {
                    console.log(t)
                }
            }),
            app.util.request({
                url: 'entry/wxapp/getad',
                cachetime: '0',
                data: {
                    type: 1
                },
                success: function (t) {
                    0 < t.data.length &&
                        e.setData({
                            getad: t.data
                        })
                }
            }),
            app.util.request({
                url: 'entry/wxapp/JdList',
                cachetime: '0',
                data: {
                    cityName: wx.getStorageSync('cityName'),
                    rec: '1', //预留推荐字段
                    longitude: wx.getStorageSync('longitude'),
                    latitude: wx.getStorageSync('latitude')
                },
                success: function (t) {
                    if (0 < t.data.length) {
                        d = d.concat(t.data)
                        for (var e1 = 0; e1 < d.length; e1++) {
                            var a = d[e1].coordinates.split(',');
                            (d[e1].lat2 = a[0]), (d[e1].lng2 = a[1])
                            var i = e.data.lat1,
                                n = e.data.lng1,
                                o = Number(a[0]),
                                r = Number(a[1])
                            // d[e1].distance = app.util.location(i, o, n, r)
                            // d[e1].distance = (d[e1]?.distance?.split('.')[0])
                        }
                        e.setData({
                            hotel: d
                        })
                    } else {
                        e.setData({
                            hotel: []
                        })
                    }
                }
            }),
            e.date()
    },
    search: function (t) {
        wx.navigateTo({
            url: 'search',
            success: function (t) {},
            fail: function (t) {},
            complete: function (t) {}
        })
    },
    copyright: function (t) {
        var e = this.data.platform.tz_appid
        wx.navigateToMiniProgram({
            appId: e,
            success: function (t) {}
        })
    },
    bindDateChange1: function (t) {
        var e = t.detail.value,
            a = app.util.addDate(t.detail.value, 1),
            i = (this.data.current_date, app.getTime2Time(a, e))
        wx.setStorageSync('day1', e),
            wx.setStorageSync('day2', a),
            wx.setStorageSync('day', i),
            console.log(i),
            console.log(app.util.addDate(t.detail.value, 1)),
            this.setData({
                datein: t.detail.value,
                dateout: app.util.addDate(t.detail.value, 1),
                time: i
            })
    },
    bindDateChange2: function (t) {
        var e = this.data.datein,
            a = t.detail.value,
            i = app.getTime2Time(a, e)
        wx.setStorageSync('day1', e),
            wx.setStorageSync('day2', a),
            wx.setStorageSync('day', i),
            this.setData({
                dateout: t.detail.value,
                time: i
            })
    },
    wode: function (t) {
        wx.reLaunch({
            url: '../logs/logs'
        })
    },
    content: function (t) {
        let e = this.data.platform
        let classifyid = this.data.classifyid
        if (2 == e.type) {
            wx.getLocation({
                type: 'wgs84',
                success: function (t) {
                    var e = t.latitude,
                        a = t.longitude;
                    (_location = e + ',' + a),
                    wx.setStorageSync('latitude', t.latitude),
                        wx.setStorageSync('longitude', t.longitude),
                        console.log('classifyid: ' + classifyid)
                    wx.navigateTo({
                        url: '../hotel_list/hotel_list?nearby=0&classifyid=' + classifyid
                    })
                    e.refresh()
                    e.setTabBarSelected()
                },
                fail: function (t) {
                    wx.navigateTo({
                        url: '../hotel_list/hotel_list?nearby=0&classifyid=' + 1
                    })
                }
            })
        } else {
            wx.navigateTo({
                url: '../hotel_list/hotel_info?hotel_id=' + e.seller_id + '&type=1'
            })
        }
    },
    location: function (t) {
        if ('1' != this.data.platform.openCity) {
            wx.showLoading({
                title: '搜索附近酒店'
            })
            wx.getLocation({
                type: 'wgs84',
                success: function (t) {
                    var e = t.latitude,
                        a = t.longitude;
                    (_location = e + ',' + a),
                    wx.setStorageSync('latitude', t.latitude),
                        wx.setStorageSync('longitude', t.longitude),
                        wx.navigateTo({
                            url: '../hotel_list/hotel_list?nearby=1'
                        })
                },
                fail: function (t) {
                    wx.hideLoading(),
                        (_location = 1),
                        wx.showModal({
                            title: '授权提示',
                            content: '您取消了本次授权，小程序将无法正常使用，请点击确定或者在我的-授权管理中进行授权，再次重新进入小程序即可',
                            showCancel: !0,
                            cancelText: '取消',
                            cancelColor: '#333',
                            confirmText: '确定',
                            confirmColor: '#333',
                            success: function (t) {},
                            fail: function (t) {},
                            complete: function (t) {}
                        })
                }
            })
        } else {
            wx.navigateTo({
                url: '../switchcity/switchcity?cityName=' + this.data.weizhi
            })
        }
    },
    dwCity: function () {
        var i = this
        // if(wx.getStorageSync("cityName")){
        //     i.setData({
        //         weizhi: wx.getStorageSync("cityName")
        //     })

        // }else{
        //     return
        // }
        wx.showLoading({
                title: '定位中'
            }),
            wx.getLocation({
                // type: "wgs84",
                type: 'gcj02',
                success: function (t) {
                    var e = t.latitude,
                        a = t.longitude
                    console.log(e, a, 'eeaa')
                    qqmapsdk.reverseGeocoder({
                        location: {
                            latitude: e,
                            longitude: a
                        },
                        coord_type: 1,
                        success: function (t) {
                            console.log(t, '看看ttt')
                            i.setData({
                                    weizhi: t.result.address_component.city
                                }),
                                wx.setStorageSync('cityName', t.result.address_component.city),
                                wx.hideLoading(),
                                console.log(t)
                        }
                    })
                }
            }),
            console.log('dwcity')
    },
    changeClassify: function (t) {
        var e = this.data.classifyid
        var a = t.currentTarget.dataset.classifyid
        this.setData({
            classifyid: a,
            sliderOffset: (a - 1) * 250
        })
    },

    onShow: function () {
        var e = this
        e.date()
        e.setData({
            weizhi: wx.getStorageSync('cityName')
        })
        console.log('当前的位置：', e.data.weizhi, wx.getStorageSync('cityName'))
        e.refresh()
        e.setTabBarSelected()
    },
    /**
     * @function 设置自定义tabbar选中状态
     */
    setTabBarSelected() {
        if (typeof this.getTabBar === 'function') {
            this.getTabBar(tabBar => {
                tabBar.setData({
                    selected: 0
                })
            })
        }
    },
    onPullDownRefresh: function () {
        this.refresh(), wx.stopPullDownRefresh()
    },
    conlist: function (t) {
        wx.navigateTo({
            url: '/zh_jdgjb/pages/hotel_list/hotel_info?hotel_id=' +
                t.currentTarget.dataset.id +
                '&type=1'
        })
    }
})